﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Configuration;
using System.Reflection;


namespace Anuntiomatic.Server
{
    public class AppSetting
    {

        public static string ConnectionString
        {
            get { return System.Configuration.ConfigurationManager.ConnectionStrings["AutoAnuntiomaticConnectionString"].ConnectionString; }
            set
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
                connectionStringsSection.ConnectionStrings["AutoAnuntiomaticConnectionString"].ConnectionString = value;
                config.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
                _SqlConnectionStringBuilder = new SqlConnectionStringBuilder(value);
            }
        }
       
        public static SqlConnection GetConnection()
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString);
                conn.Open();
                return conn;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static SqlConnectionStringBuilder _SqlConnectionStringBuilder;
        public static SqlConnectionStringBuilder ConnectionStringBuilder
        {
            get
            {
                if (_SqlConnectionStringBuilder == null) _SqlConnectionStringBuilder = new SqlConnectionStringBuilder(ConnectionString);
                return _SqlConnectionStringBuilder;
            }
        }

        public static string DataSource
        {
            get
            {
                return ConnectionStringBuilder.DataSource;
            }
            set
            {
                ConnectionStringBuilder.DataSource = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string Database
        {
            get
            {
                return ConnectionStringBuilder.InitialCatalog;
            }
            set
            {
                ConnectionStringBuilder.InitialCatalog = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string UserID
        {
            get
            {
                return ConnectionStringBuilder.UserID;
            }
            set
            {
                ConnectionStringBuilder.UserID = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string Password
        {
            get
            {
                return ConnectionStringBuilder.Password;
            }
            set
            {
                ConnectionStringBuilder.Password = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string GetAppConfig(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key] ?? "";
        }

        public static void SetAppConfig(string key, string value)
        {
            try
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                ((AppSettingsSection)config.GetSection("appSettings")).Settings[key].Value = value;
                config.Save();
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception)
            {
                throw;
            }
        }
       
    }
}
